package com.wonyesoft.wuliu.acticity;

import httptools.HttpJsonTool;

import java.io.File;
import java.io.ObjectOutputStream.PutField;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javabeen.CarTypeBeen;

import org.json.JSONException;
import org.json.JSONObject;

import adapter.CarTypeListAdapter;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.SearchManager.OnCancelListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.utils.DistanceUtil;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.acticity.order.AddCargoOrderActivity;
import com.wonyesoft.wuliu.acticity.utils.FileUtils;
import com.wonyesoft.wuliu.acticity.utils.PhotoUtils;
import com.wonyesoft.wuliu.acticity.utils.StringUtils;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.model.CarType;
import com.wonyesoft.wuliu.model.CargoInfo;
import com.wonyesoft.wuliu.view.cpbutton.PayDialog;
import com.wonyesoft.wuliu.view.cpbutton.PayDialog.OnCustomDialogListener;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.JsonResponse;
import com.wonyesoft.wuliu.vo.State;

public class ChoseCarTypeActivity extends BaseActivity implements
		OnClickListener{

	
	private TextView title, head_right, cargoinfo_cargotypetxt,
			cargoinfo_cargovolumetxt, cargoinfo_cargoweighttxt, driver_time,
			cargoinfo_cargocartypetxt, cargoinfo_cargopay1, driver_guige,
			cargoinfo_cargopay2, a11_common, a12_common,
			cargoinfo_cargocartypetxtisback;
	private ImageView head_left, driver_imgone, driver_imgtwo, driver_imgthree,
			addcargo_paytype_img, addcargo_payfang_img, addcargo_huidan_img;
	private EditText driver_carnumber, driver_youhao, driver_leixing,
			driver_long, a8_common;
	private Button driver_baochun, a9_common, a10_common;
	private String passwordString, rePwdString, repwd2String, driverID;
	private RequestParams params;
	private LinearLayout cargoinfo_shrmap, cargoinfo_fhrmap, driver_zaizhong,
			bytxt, bxtxt, hdtxt;
	private RelativeLayout t0,t1,t2,t3,t4,t5,t6;
	/**
	 * 汽车类型
	 */
	private List<CarTypeBeen> carTypeBeens;

	/**
	 * 汽车列表
	 */
	private ListView car_type_list_view;
	
	private AppContent app;
	
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chose_car_type);
		app=(AppContent) getApplication();
		GetCaryType();
		showLoadingDialog("加载车型中");
		title = (TextView) findViewById(R.id.head_title);
		head_left = (ImageView) findViewById(R.id.head_left);
		head_left.setVisibility(View.VISIBLE);
		head_left.setOnClickListener(this);
		title.setText("选择车辆类型");
		car_type_list_view=(ListView) findViewById(R.id.car_type_list_view);
	}

	/**
	 * 这里从服务器获取车辆类型
	 */
	private void GetCaryType(){
		HttpJsonTool httpJsonTool=new HttpJsonTool(this);
		HashMap< String, Object> data=new HashMap<String, Object>();
		httpJsonTool.SetOnLister(new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				dismissLoadingDialog();
				if (msg.what==1) {
					carTypeBeens=(List<CarTypeBeen>) msg.obj;
					IniData();
				}
			}
		});
		Type type=new TypeToken<List<CarTypeBeen>>(){}.getType();
		Log.v("连接的地址", GagApi.HOST+"moduls/shippers/getCarType");
		httpJsonTool.StartWork(data, GagApi.HOST+"shippers/getCarType", type, null);
	}
	
	/**
	 * 进行数据INI
	 */
	private void IniData(){
		if (carTypeBeens.size()>0) {
			CarTypeListAdapter adapter=new CarTypeListAdapter(this,carTypeBeens,R.layout.car_type_items);
			car_type_list_view.setAdapter(adapter);
			car_type_list_view.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					Intent intent=new Intent(ChoseCarTypeActivity.this,AddCargoOrderActivity.class);
					intent.putExtra("car_type_id", carTypeBeens.get(arg2).getCartypeid());
					startActivity(intent);
					ChoseCarTypeActivity.this.finish();
				}
				
			});
		}
		
		
	}

	
	
	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.head_left:
			finish();
			break;
		case R.id.t0:
			goNext(1);
			break;
		case R.id.t1:
			goNext(2);
			break;
		case R.id.t2:
			goNext(3);
			break;
		case R.id.t3:
			goNext(4);
			break;
		case R.id.t4:
			goNext(5);
			break;
		case R.id.t5:
			goNext(6);
			break;
		case R.id.t6:
			goNext(7);
			break;
		default:
			break;
		}
		
	}


	private void goNext(int i) {
		// TODO Auto-generated method stub
		
		/**
		 * 获得的百度推送ID给服务器
		 */
			HashMap<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put("channelid", app.channelId);
			dataMap.put("userid", "null");
			dataMap.put("cargoownerid", app.userId);
			HttpJsonTool httpJsonTool = new HttpJsonTool(this);
			httpJsonTool.SetOnLister(new Handler() {
				@Override
				public void handleMessage(Message msg) {
					// TODO Auto-generated method stub
					super.handleMessage(msg);
					Log.v("上传完成", "msg-->"+(String)msg.obj);
				}
			});
			httpJsonTool.StartWork(dataMap, GagApi.PUSH_UP_DATA_URL, null, null);
			if (app.userinfobeen != null) {

			} else {
				Intent intent = new Intent(this, UserLoginActivity.class);
				startActivity(intent);
			}	
		Intent intent = new Intent(this,AddCargoLastActivity.class);
		intent.putExtra("intCarType", i);
		startActivity(intent);
		finish();
	}

}
